package com.js.study.datastructs.version1;

import java.net.URI;

/**
 * @Author JiangSen
 * @DATE 2023/12/23 9:35
 */
public class MyArray {
    int[] data;//data
    int size;//current size
    int capacity;
    public MyArray() {
        this(5);
    }//调用了有参构造,  对data进行初始化.
    public MyArray(int capacity) {
        data = new int[capacity];
        size = 0;
        this.capacity=capacity;
    }
    //getter
    public int getSize() {
        return size;
    }
    public int getCapacity() {
        this.capacity = data.length;
        return capacity;
    }
    public boolean isEmpty() {
        return size == 0;
    }
    public boolean isFull() {
        return size == data.length;
    }
    // 增加  前中后
    public void addFirst(int e) {
        add(0, e);
    }
    public void addLast(int e) {
        add(size, e);
    }
    public void add(int index, int e) {
        if (isFull()) {
            throw new RuntimeException("array is full,don't add element");
        }
        if (index < 0 || index > size) {
            throw new RuntimeException("index is not in size");
        }
        for(int i = size-1;i>=index;i--){
            data[i+1]=data[i];
        }
        data[index] = e;
        size++;
    }
    //删除   前中后问题
    public int removeFirst(){
        return remove(0);
    }
    public int removeLast(){
        return remove(size-1);
    }
    public int remove(int index){
        if(isEmpty()){
            throw new RuntimeException("array is empty,don't delete element");
        }
        if(index < 0 || index>=size){
            throw new RuntimeException("index not in size");
        }
        int result = data[index];
        for(int i = index+1;i<size;i++){
            data[i-1] = data[i];
        }
        size--;
        return result;
    }
    //  update
    public int set(int index, int e){
        if(index<0 || index >size){
            throw new RuntimeException("index not in size");
        }
        return data[index] = e;
    }
    //select
    public int get(int index) {
        if (index < 0 || index > size) {
            throw new RuntimeException("索引不在边界范围");
        }
        return data[index];
    }

    //8.含
    public boolean contain(int e) {
        for (int i = 0; i < size; i++) {
            if (data[i] == e) {
                return true;
            }
        }
        return false;
    }
    //11.正打印
    public void print() {
        for (int i = 0; i < data.length; i++) { //游离对象,后面解决
            System.out.println("data["+i+"] = " + data[i]);
        }
        /*for (int i = 0; i < size; i++) {
            System.out.println("data[i] = " + data[i]);
        }*/
    }
    //12.反打印
    public void printReverse() {
        for (int i = size - 1; i >= 0; i--) {
            System.out.println("data[i] = " + data[i]);
        }
    }
}
